//
//  DFWelcomeViewController.swift
//  FlyBlog
//
//  Created by FEI on 16/4/12.
//  Copyright © 2016年 FEI. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class DFWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        prepareUI()
        setIcon()
        DFUserAccountModel.shareInstance.loadUserInfo { (error) -> () in
            if error != nil {
                return
            }
            self.setIcon()
        }
    }

    private func setIcon() {
        if let avatar_large = DFUserAccountModel.shareInstance.userAccount?.avatar_large {
            let url = NSURL(string: avatar_large)!
            self.iconView.sd_setImageWithURL(url,placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewWillAppear(animated)
        iconViewAnimation()
    }

    private func iconViewAnimation() {
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
        }

        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: {(_) -> Void in
                        let controller = DFMainViewController()
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(controller)                })
        }

    }


    private func prepareUI() {

        view.addSubview(backgroundImg)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)

        backgroundImg.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false

        //约束
        backgroundImg.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }

        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
        }

        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }

    }


    //MARK:懒加载控件
    private lazy var iconView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()

    private lazy var backgroundImg: UIImageView = UIImageView(image: UIImage(named: "ad_background"))


    private lazy var welcomeLabel:UILabel = {
        let label = UILabel()

        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(18)
        label.sizeToFit()
        return label
    }()
}
